package com.xn.app.muying.activity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jumper.fetalheart.line.l;
import com.xn.app.muying.ChongZhiMiMaActivity;
import com.xn.app.muying.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.ChanJianXiangQingAdapter;
import com.xn.app.muying.bean.ChanJianMessageBean;
import com.xn.app.muying.util.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Request;

/**
 * Created by Administrator on 2016/10/27.
 */
public class ChanJianXiangQingActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_title,tv_isFinish,tv_alltime,tv_aleadytime,tv_endtime,tv_keypoint,tv_requiredItems,tv_optionalItems;
    public static TextView tv_info,tv_message;
//    private Button btn_one,btn_two,btn_three,btn_four;
    private ArrayList<ChanJianMessageBean> list1 = new ArrayList<ChanJianMessageBean>();;
    private ArrayList<ChanJianMessageBean> list2 = new ArrayList<ChanJianMessageBean>();;
    private GridView gridView;
    private String hospitalId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chanjianxiangqing);
        ImageView back = (ImageView) findViewById(R.id.activity_chanjianxiangqing_back);
        Intent intent = getIntent();
        String nextAntenatalCareId = intent.getStringExtra("nextAntenatalCareId");
        loadMessage(nextAntenatalCareId);

        initView();

        back.setOnClickListener(this);
        tv_requiredItems.setOnClickListener(this);
        tv_optionalItems.setOnClickListener(this);


    }

    private void initView() {
        tv_title= (TextView) findViewById(R.id.activity_chanjianxiangqing_title);
        tv_isFinish= (TextView) findViewById(R.id.activity_chanjianxiangqing_isFinish);
        tv_alltime= (TextView) findViewById(R.id.activity_chanjianxiangqing_alltime);
        tv_aleadytime= (TextView) findViewById(R.id.activity_chanjianxiangqing_aleadytime);
        tv_endtime= (TextView) findViewById(R.id.activity_chanjianxiangqing_endtime);
        tv_keypoint= (TextView) findViewById(R.id.activity_chanjianxiangqing_keypoint);
        tv_requiredItems= (TextView) findViewById(R.id.activity_chanjianxiangqing_requiredItems);
        tv_optionalItems= (TextView) findViewById(R.id.activity_chanjianxiangqing_optionalItems);

        tv_info= (TextView) findViewById(R.id.activity_chanjianxiangqing_bottom_info);
        tv_message= (TextView) findViewById(R.id.activity_chanjianxiangqing_bottom_message);

        gridView= (GridView) findViewById(R.id.activity_chanjianxiangqing_gridview);


    }



    private void loadMessage(final String nextAntenatalCareId) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    String tokens = Constants.getStringByName(ChanJianXiangQingActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    Request request = new Request.Builder().url("http://apitest.beierbaby.com/api/antenatalcares/user/antentalcare/detail/"+nextAntenatalCareId).addHeader("Authorization", "Bearer " + token.get("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    String bodys = response.body().string();
                    onJsonMessage(bodys);
                    Log.i("bodysmessage",bodys);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    String hospitalName=null;
    private void onJsonMessage(String bodys) {
        try {
            JSONObject jsonObject = new JSONObject(bodys);
            final boolean isFinish = jsonObject.optBoolean("isFinish");
            final String pregnancyWeek = jsonObject.optString("pregnancyWeek");
            final String pregnancyDay = jsonObject.optString("pregnancyDay");
            final String fromThePregnancyStartDay = jsonObject.optString("fromThePregnancyStartDay");
            String fromThePregnancyEndDay = jsonObject.optString("fromThePregnancyEndDay");
            String tipMessage = jsonObject.optString("tipMessage");
            final String title = jsonObject.optString("title");
            hospitalId = jsonObject.optString("hospitalId");
            hospitalName = jsonObject.optString("hospitalName");
            String order = jsonObject.optString("order");
            final String startWeek = jsonObject.optString("startWeek");
            final String startDay = jsonObject.optString("startDay");
            final String endWeek = jsonObject.optString("endWeek");
            final String endDay = jsonObject.optString("endDay");
            final String keyPoint = jsonObject.optString("keyPoint");
            String id = jsonObject.optString("id");




            JSONArray requiredItems = jsonObject.getJSONArray("requiredItems");

            for (int i = 0; i < requiredItems.length(); i++) {
                JSONObject jsonObject1 = requiredItems.getJSONObject(i);
                String req_name = jsonObject1.optString("name");
                String req_explain = jsonObject1.optString("explain");
                String req_tip = jsonObject1.optString("tip");
                String req_id = jsonObject1.optString("id");
                ChanJianMessageBean chanJianMessageBean = new ChanJianMessageBean();
                chanJianMessageBean.setName(req_name);
                chanJianMessageBean.setExplain(req_explain);
                chanJianMessageBean.setTip(req_tip);
                chanJianMessageBean.setId(req_id);
                list1.add(chanJianMessageBean);
            }
            JSONArray optionalItems = jsonObject.getJSONArray("optionalItems");
//            list2 = new ArrayList<ChanJianMessageBean>();
            for (int i = 0; i < optionalItems.length(); i++) {
                JSONObject jsonObject1 = optionalItems.getJSONObject(i);
                String opt_name = jsonObject1.optString("name");
                String opt_explain = jsonObject1.optString("explain");
                String opt_tip = jsonObject1.optString("tip");
                Log.i("opt_tip",opt_tip);
                String opt_id = jsonObject1.optString("id");
                ChanJianMessageBean chanJianMessageBean = new ChanJianMessageBean();
                chanJianMessageBean.setName(opt_name);
                chanJianMessageBean.setExplain(opt_explain);
                chanJianMessageBean.setTip(opt_tip);
                chanJianMessageBean.setId(opt_id);
                list2.add(chanJianMessageBean);
            }


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_title.setText(title);
                    if(isFinish){
                        tv_isFinish.setText("已做");
                    }else{
                        tv_isFinish.setText("未做");
                    }
                    tv_alltime.setText("产检时间 （孕"+startWeek+startDay+"————"+endWeek+endDay+"周）");
                    tv_aleadytime.setText("你目前已怀孕"+pregnancyWeek+"周"+pregnancyDay+"天");
                    tv_endtime.setText("距离"+title+"还有"+fromThePregnancyStartDay+"天");
                    tv_keypoint.setText(keyPoint);
                    tv_optionalItems.setBackgroundResource(R.drawable.button_white_kepu2);
                    tv_requiredItems.setBackgroundResource(R.drawable.button_red_kepu1);
                    ChanJianXiangQingAdapter requiredItemsAdapter=new ChanJianXiangQingAdapter(ChanJianXiangQingActivity.this,list1);
                    gridView.setAdapter(requiredItemsAdapter);
                }

            });



        } catch (JSONException e) {
            e.printStackTrace();
        }
    }





    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        Resources resource = (Resources) getBaseContext().getResources();
        switch (view.getId()){
            case R.id.activity_chanjianxiangqing_back:
                intent.setClass(ChanJianXiangQingActivity.this,ChanJianListActivity.class);
                intent.putExtra("hospitalId",hospitalId);
                startActivity(intent);
                finish();
                break;

            case R.id.activity_chanjianxiangqing_requiredItems:
                tv_optionalItems.setBackgroundResource(R.drawable.button_white_kepu2);
                tv_requiredItems.setBackgroundResource(R.drawable.button_red_kepu1);

                ColorStateList csl_optionalItems = (ColorStateList) resource.getColorStateList(R.color.sys_fen);
                ColorStateList csl_requiredItems = (ColorStateList) resource.getColorStateList(R.color.white);
                tv_requiredItems.setTextColor(csl_requiredItems);
                tv_optionalItems.setTextColor(csl_optionalItems);

                    final ChanJianXiangQingAdapter requiredItemsAdapter=new ChanJianXiangQingAdapter(ChanJianXiangQingActivity.this,list1);
                    gridView.setAdapter(requiredItemsAdapter);


                break;
            case R.id.activity_chanjianxiangqing_optionalItems:

//                button_white_kepu2
                tv_optionalItems.setBackgroundResource(R.drawable.button_red_kepu2);
                tv_requiredItems.setBackgroundResource(R.drawable.button_white_kepu1);

                ColorStateList csl_optionalItems1 = (ColorStateList) resource.getColorStateList(R.color.white);
                ColorStateList csl_requiredItems1 = (ColorStateList) resource.getColorStateList(R.color.sys_fen);
                tv_requiredItems.setTextColor(csl_requiredItems1);
                tv_optionalItems.setTextColor(csl_optionalItems1);
            final ChanJianXiangQingAdapter optionalItemsAdapter=new ChanJianXiangQingAdapter(ChanJianXiangQingActivity.this,list2);
            gridView.setAdapter(optionalItemsAdapter);

                break;
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
